package com.example.javamaday.codetop;

import com.example.javamaday.leetcode.base.ListNode;

public class CodeTop0141 {

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        boolean hasCycle = false;
        while (true) {
            if (fast == null || slow == null) {
                break;
            }
            if (fast.next == null) {
                break;
            } else {
                fast = fast.next.next;
                slow = slow.next;
            }
            if (fast == slow) {
                hasCycle = true;
                break;
            }
        }
        return hasCycle;
    }

}
